﻿#region Copyright Notice
// This file is part of GestionPedago.
// <description>
// Copyright (C) 2012 ENI Ecole Informatique
// 
// GestionPedago is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// <program> is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GestionPedago.EntityContext;
using GestionPedago.EntityContext.EntityOPTools;

namespace GestionPedago.Persistence.OPManager
{
    /// <summary>
    /// Manager de persistance de l'Entity Competence
    /// </summary>
    public abstract class CompetenceOPManager
    {
        /// <summary>
        /// Méthode de récupération de la liste des compétences
        /// </summary>
        /// <param name="recherche">Filtre de recherche</param>
        /// <param name="orderByLibelleAsc">Vrai pour un tri Ascendant</param>
        /// <returns></returns>
        public static List<Competence> GetListeCompetence(string recherche = null, bool orderByLibelleAsc = true)
        {
            try
            {
                IQueryable<Competence> query = EntityConnection.GetContext().Competence;

                query.Where(it => (recherche == null || recherche.Trim() == "" || it.Libelle.ToUpper().Contains(recherche.ToUpper())));

                if (orderByLibelleAsc)
                {
                    query.OrderBy(it => it.Libelle);
                }
                else
                {
                    query.OrderByDescending(it => it.Libelle);
                }
                query.Select(it => it);

                return query.ToList();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Méthode de recherche d'une compétence par son id
        /// </summary>
        /// <param name="id">L'id de la compétence recherchée</param>
        /// <returns>La compétence</returns>
        public static Competence GetCompetenceById(int id)
        {

            try
            {
                var query = from it in EntityConnection.GetContext().Competence
                            where it.IdCompetence == id
                            select it;

                Competence c = null;

                c = query.First();

                return c;

            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        //public static List<Competence> GetListeCompetencesByIdECF(int idECF)
        //{

        //    try
        //    {
        //        var query = from it in EntityConnection.GetContext().Competence
        //                    select it;

        //        List<Competence> lst = new List<Competence>();

        //        foreach (Competence c in query)
        //        {
        //            ECF e = new ECF();
        //            e.IdECF = idECF;
        //           /* if(!EntityStateChecker.IsDetached(e)){
        //                 EntityConnection.GetContext().ECF.Detach(e);
        //            }*/
        //            if (c.ECF.Contains(e))
        //            {
        //                lst.Add(c);
        //            }
        //        }

        //        return lst;

        //    }
        //    catch (Exception ex)
        //    {
        //        throw ex;
        //    }

        //}

        /// <summary>
        /// Méthode d'ajout d'une compétence en base
        /// </summary>
        /// <param name="c">La compétence à ajouter</param>
        /// <returns>La compétence</returns>
        public static Competence AjouterCompetence(Competence c)
        {
            try
            {
                if (!EntityStateChecker.IsDetached(c))
                {
                    EntityConnection.GetContext().Competence.Detach(c);
                }
                EntityConnection.GetContext().Competence.AddObject(c);
                EntityConnection.GetContext().SaveChanges();
                return c;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Méthode de suppression d'une compétence en base
        /// </summary>
        /// <param name="c">La compétence à supprimer</param>
        public static void SupprimerCompetence(Competence c)
        {
            try
            {
                Competence co = CompetenceOPManager.GetCompetenceById(c.IdCompetence);
                EntityConnection.GetContext().Competence.DeleteObject(co);
                EntityConnection.GetContext().SaveChanges();
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }
    }
}
